<?php
class sample_master_model extends MY_Model {
	var $model = 'sample_master';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	a.pk_master, a.field1, TO_CHAR(a.field2,'DD/MM/YYYY') field2, TO_CHAR(a.field3,'DD/MM/YYYY') field3, a.field4, a.field5, a.field6, a.field7, a.ref_key_combo, a.ref_key_lov, a.province_code, a.city_code, a.create_date, a.create_id, a.modify_date, a.modify_id, 
							b.ref_name ref_name_combo, c.ref_name ref_name_lov, d.province_name, e.city_name
					FROM	sample_master a
							LEFT JOIN sample_reference b ON a.ref_key_combo = b.ref_key 
							LEFT JOIN sample_reference c ON a.ref_key_lov = c.ref_key
							LEFT JOIN sw_province d ON a.province_code = d.province_code
							LEFT JOIN sw_city e ON a.city_code = e.city_code
					WHERE	(field1 LIKE '%".post('keyword')."%')
					ORDER BY ".(post('sort')=='' ? 'a.pk_master' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetImportJSON()
	{
		$ls_sql	= "	SELECT	sessid, index_no, field1, TO_CHAR(field2,'DD/MM/YYYY') field2, TO_CHAR(field3,'DD/MM/YYYY') field3, field4, field5
					FROM	sample_import
					WHERE	(field1 LIKE '%".post('keyword')."%')
					ORDER BY ".(post('sort')=='' ? 'index_no' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('sample_master', $arr_data);
		
		$this->AuditTrail(	'sample_master',// Table
							'Create',		// Action
							''				// Primary Key
							);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('sample_master', $arr_data, $arr_rule);
		
		$this->AuditTrail(	'sample_master',		// Table
							'Update',				// Action
							json_encode($arr_rule)	// Primary Key
							);
	}
	
	function GetSingle($as_pkmaster){
		$result = $this->db->get_where('sample_master', array('ref_key' => $as_pkmaster));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_pkmaster)
	{
		$result	= $this->db->query("SELECT	a.pk_master, a.field1, TO_CHAR(a.field2,'DD/MM/YYYY') field2, TO_CHAR(a.field3,'DD/MM/YYYY') field3, a.field4, a.field5, a.field6, a.field7, a.ref_key_combo, a.ref_key_lov, a.province_code, a.city_code, a.create_date, a.create_id, a.modify_date, a.modify_id, 
											b.ref_name ref_name_combo, c.ref_name ref_name_lov, d.province_name, e.city_name
									FROM	sample_master a
											LEFT JOIN sample_reference b ON a.ref_key_combo = b.ref_key
											LEFT JOIN sample_reference c ON a.ref_key_lov = c.ref_key
											LEFT JOIN sw_province d ON a.province_code = d.province_code
											LEFT JOIN sw_city e ON a.city_code = e.city_code
									WHERE	a.pk_master = '".$as_pkmaster."'");
		
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			$this->db->delete('sample_detail2', $arr_rule);
			$this->db->delete('sample_detail1', $arr_rule);
			$this->db->delete('sample_master', $arr_rule);
			
			$this->AuditTrail(	'sample_master',		// Table
								'Delete',				// Action
								json_encode($arr_rule)	// Primary Key
								);
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>